package cn.gjculture.shopping.api.service.impl;

import cn.gjculture.shopping.api.dao.CommonUpdateMapper;
import cn.gjculture.shopping.api.dao.MerchantDao;
import cn.gjculture.shopping.api.entity.InterfaceParam;
import cn.gjculture.shopping.api.entity.Merchant;
import cn.gjculture.shopping.api.entity.Return;
import cn.gjculture.shopping.api.service.InterfaceService;
import cn.gjculture.shopping.api.service.MerchantService;
import cn.gjculture.shopping.api.util.DateUtil;
import cn.gjculture.shopping.api.util.UUIDUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class MerchantServiceImpl implements MerchantService {





    @Autowired
    MerchantDao merchantDao;

    @Autowired
    CommonUpdateMapper commonUpdateMapper;

    @Autowired
    InterfaceService interfaceService;

    @Override
    public int save(Merchant mer, Return ret) {
        mer.setMerchantId(UUIDUtil.getUUID());
        mer.setInsertTime(DateUtil.getTimestamp());
        mer.setUpdateTime(DateUtil.getTimestamp());
//        mer.setMerType("per");
        mer.setStatus(2);
        Merchant merchant=merchantDao.queryByPhone(mer.getPhone());
        if(merchant==null){
            merchantDao.save(mer);
            InterfaceParam re=interfaceService.getRegisterParam(mer);
            ret.setData(re);
        }else {
            ret.getCommon().setRet(false);
            ret.getCommon().setMessage("手机号已经注册");
            return 0;
        }

        return 1;
    }

    @Override
    public int back(String code,String msg, JSONObject responseParam) {
        Merchant merchant=new Merchant();
        merchant.setSonMerNo(responseParam.getString("sonMerNo"));
        merchant.setStatus(Integer.valueOf(responseParam.getString("status")));
        merchant.setMerchantId(responseParam.getString("seraialNumber"));
        merchant.setUpdateTime(DateUtil.getTimestamp());
        merchantDao.updateMerNoAndStauts(merchant);
        return 1;
    }

    @Override
    public Merchant queryByBelongId(String id) {
        return merchantDao.queryByBelongId(id);
    }

    @Override
    public List<Merchant> query() {
        return merchantDao.query();
    }

    @Override
    public int update(Merchant merchant, Return ret) {
        merchant.setUpdateTime(DateUtil.getTimestamp());
        commonUpdateMapper.updateMerchant(merchant);
        Merchant mer=merchantDao.queryById(merchant.getMerchantId());
        InterfaceParam re=interfaceService.getRegisterParam(mer);
        ret.setData(re);
        return 1;
    }

    @Override
    public Merchant queryById(String supplierId) {
        return merchantDao.queryById(supplierId);
    }

    @Override
    public int registerAgain(Merchant merchant, Return ret) {
        merchant=merchantDao.queryById(merchant.getMerchantId());
        InterfaceParam re=interfaceService.getRegisterParam(merchant);
        ret.setData(re);
        return 1;
    }

    @Override
    public List<Merchant> querySystem() {
        return merchantDao.querySystem();
    }


}
